#!/bin/sh -efu
#
# Check patching of whole repo index into another repo index.
#
# Copyright (C) 2021 Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later

. "${srcdir=.}/init.sh"

[ -d "${ART_HEAVY_TESTDATA_DIR:-}" ] ||
	skip_ 'heavy test data is not found in $ART_HEAVY_TESTDATA_DIR'

FROM="$1"; shift
TO="$1"; shift

if [ "$FROM" = "$TO" ]; then
	from_repo="$ART_HEAVY_TESTDATA_DIR"/"$FROM"/prev
else
	from_repo="$ART_HEAVY_TESTDATA_DIR"/"$FROM"/latest
fi
to_repo="$ART_HEAVY_TESTDATA_DIR"/"$TO"/latest

[ -d "$from_repo" ]
[ -d "$to_repo" ]

repo_expected=$to_repo

skip_compare_release=yes
strip_size_md5=yes
basedir=base.bloat

for arch in x86_64 noarch; do
	copy_pkgs "$arch"

	comps="$(set +f && cd "$repo/$arch" && ls -d RPMS.?* | sed 's/^RPMS\.//')"
	[ -n "$comps" ]

	parse_release "$to_repo/$arch/$basedir"/release
	architectures="$_Architectures"
	codename="$_Codename"
	description="$_Description"
	label="$_Label"
	origin="$_Origin"
	suite="$_Suite"

	parse_release "$to_repo/$arch/$basedir"/release.classic
	archive="$_Archive"
	version="$_Version"

	xzdiff "$from_repo/files/list/$arch.hash.xz" "$to_repo/files/list/$arch.hash.xz" \
		> "$tmpdir"/"$arch".hash.diff ||:
	sed -n 's/^< [^ ]*  \(.*\)/\1/p' "$tmpdir"/"$arch".hash.diff | \
		sort > "$tmpdir"/"$arch".rm
	sed -n 's/^> [^ ]*  \(.*\)/\1/p' "$tmpdir"/"$arch".hash.diff | \
		sort > "$tmpdir"/"$arch".add

	xz -d < "$from_repo"/files/list/bin.list.xz | sort -k4,4 > "$tmpdir"/bin.list.from
	xz -d < "$to_repo"/files/list/bin.list.xz | sort -k4,4 > "$tmpdir"/bin.list.to

	join -2 4 "$tmpdir"/"$arch".rm "$tmpdir"/bin.list.from -o '2.5 2.4' \
		> "$tmpdir"/rm-src-bin

	join -2 4 "$tmpdir"/"$arch".add "$tmpdir"/bin.list.to -o '2.5 2.4' \
		> "$tmpdir"/add-src-bin

	while read srpm rpm; do
		for c in $comps; do
			if [ -f "$from_repo/$arch/RPMS.$c/$rpm" ]; then
				printf "%s\t%s\t%s\n" "$c" "$srpm" "$rpm"
				break
			fi
		done
	done < "$tmpdir"/rm-src-bin > "$tmpdir"/rm-list

	while read srpm rpm; do
		for c in $comps; do
			if [ -f "$to_repo/$arch/RPMS.$c/$rpm" ]; then
				printf "%s\t%s\t%s\n" "$c" "$srpm" "$rpm"
				break
			fi
		done
	done < "$tmpdir"/add-src-bin > "$tmpdir"/add-list

	$GENBASEDIR \
		--no-bz2 --no-xz \
		--bloat \
		--basedir "$basedir" \
		--prev-base "$from_repo/$arch/$basedir" \
		--add-list "$tmpdir"/add-list \
		--rm-list "$tmpdir"/rm-list \
		\
		--architecture="$arch" \
		--architectures="$architectures" \
		--archive="$archive" \
		--codename="$codename" \
		--description="$description" \
		--label="$label" \
		--origin="$origin" \
		--suite="$suite" \
		--version="$version" \
		\
		--create \
		--architecture="$arch" \
		--topdir="$repo" \
		--flat --mapi \
		--no-oldhashfile \
		--no-scan \
		--changelog-since=2009-01-01 \
		$arch $comps

	check_base "$arch"
done

export XZ_OPT='--lzma2=nice=128,depth=80,lc=4'
basedir=base
for arch in x86_64 noarch; do
	comps="$(set +f && cd "$repo/$arch" && ls -d RPMS.?* | sed 's/^RPMS\.//')"
	[ -n "$comps" ]

	parse_release "$to_repo/$arch/$basedir"/release
	architectures="$_Architectures"
	codename="$_Codename"
	description="$_Description"
	label="$_Label"
	origin="$_Origin"
	suite="$_Suite"

	parse_release "$to_repo/$arch/$basedir"/release.classic
	archive="$_Archive"
	version="$_Version"

	$GENBASEDIR \
		--prev-base "$repo/$arch"/base.bloat \
		--useful-files "$to_repo"/useful-files \
		\
		--architecture="$arch" \
		--architectures="$architectures" \
		--archive="$archive" \
		--codename="$codename" \
		--description="$description" \
		--label="$label" \
		--origin="$origin" \
		--suite="$suite" \
		--version="$version" \
		\
		--create \
		--architecture="$arch" \
		--topdir="$repo" \
		--flat --mapi \
		--no-oldhashfile --bz2only \
		--no-scan \
		--changelog-since=2009-01-01 \
		--xz --no-bz2 \
		$arch $comps

	check_base "$arch"
done
